/**
 * 树的CRUD操作工具按钮类
 * 鼠标移到树的节点上时显示该菜单，移出之后隐藏
 */
if (Best==undefined){
	var Best={};
}
Best.TreeToolBar = Class.create();
Best.TreeToolBar.prototype = {
	initialize: function(options) {
		this.options = Object.extend(
				{
					path: "", //tool bar button img path
					elementId: "", //附属的html元素id
					data: {},  //要修改的节点的数据对象
					isShowAddButton: true, //是否显示添加按钮
					isShowEditButton: true, //是否显示编辑按钮
					isShowDeleteButton: true, //是否显示删除按钮
					afterAdd: Prototype.emptyFunction, //添加回调函数
					afterEdit: Prototype.emptyFunction, //修改完成回调函数
					afterDelete: Prototype.emptyFunction //删除完成回调函数
			 	}, options);
		this.element = $(this.options.elementId);		 	
		this.div = null; //菜单所属div元素
		this._registEvent();
	},
	/**
	 * 显示toolbar
	 */
	show: function(){
		if(this.div == null){
			this._create();
		}
		this.div.style.display = "";
	},
	/**
	 * 隐藏toolbar
	 */
	hide: function(){
		if(this.div){
			this.div.style.display = "none";
		}
	},
	/**
	 * 停止检测onmouseover和onmouseout事件
	 */
	stopObserve: function(){
		this.element.onmouseover = null;
		this.element.onmouseout = null;
	},
	/**
	 * 停止检测onmouseover和onmouseout事件
	 */
	startObserve: function(){
		this._registEvent();
	},
	_registEvent: function(){
		if(this.element != undefined){
			this.element.onmouseover = this.show.bind(this);
			this.element.onmouseout = this.hide.bind(this);
		}
		//this.element.onmousedown = this.hide.bind(this);
	},
	_create: function(){
		this.div = document.createElement("div");
		this.div.style.display = "none";
		this.div.className = "treeButton";
		if(this.options.isShowAddButton)
			this.div.appendChild(this._getAddButtonDiv());
		if(this.options.isShowEditButton)
			this.div.appendChild(this._getEditButtonDiv());
		if(this.options.isShowDeleteButton)			
			this.div.appendChild(this._getDeleteButtonDiv());
		
		this.element.appendChild(this.div);
	},
	/**
	 * 获取添加文件按钮的元素
	 */
	_getAddButtonDiv: function(){
		var result = document.createElement("div");
		result.onclick = this._onAddButtonClick.bind(this);
		result.innerHTML = "<img src='" + this.options.path + "/add.gif' class='treeButton'/>";
		return result;
	},
	_onAddButtonClick: function(event){
		if(event == undefined){
			event = window.event;
		}
		this.hide();
		this.options.afterAdd(this.options.elementId);
		Event.stop(event);
	},
	/**
	 * 获取编辑按钮的元素
	 */
	_getEditButtonDiv: function(){
		var result = document.createElement("div");
		result.onclick = this._onEditButtonClick.bind(this);
		result.innerHTML = "<img src='" + this.options.path + "/edit.gif' class='treeButton'/>";
		return result;
	},
	_onEditButtonClick: function(event){
		if(event == undefined){
			event = window.event;
		}
		this.hide();
		this.options.afterEdit(this.options.elementId);
		Event.stop(event);
	},
	/**
	 * 获取删除按钮的元素
	 */
	_getDeleteButtonDiv: function(){
		var result = document.createElement("div");
		result.onclick = this._onDeleteButtonClick.bind(this);
		result.innerHTML = "<img src='" + this.options.path + "/delete.gif' class='treeButton'/>";
		return result;
	},
	_onDeleteButtonClick: function(event){
		if(event == undefined){
			event = window.event;
		}
		if(window.confirm("您确定要删除: " + this.options.data.value + "?")){
			this.hide();
			this.options.afterDelete(this.options.elementId);
		}
		Event.stop(event);
	}
	
}